package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.eiahe.hr.attendance.domain.bo.AnnualLeaveAdjustBo;
import org.eiahe.hr.attendance.domain.bo.AnnualLeaveBo;
import org.eiahe.hr.attendance.domain.vo.AnnualLeaveAdjustLogVo;
import org.eiahe.hr.attendance.domain.vo.AnnualLeaveVo;
import org.eiahe.hr.attendance.service.IAnnualLeaveService;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 年假核算管理
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/annualLeave")
public class AnnualLeaveController extends BaseController {

    private final IAnnualLeaveService annualLeaveService;

    /**
     * 查询年假核算列表
     */
    @SaCheckPermission("attendance:annualLeave:list")
    @GetMapping("/list")
    public TableDataInfo<AnnualLeaveVo> list(AnnualLeaveBo bo, PageQuery pageQuery) {
        return annualLeaveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出年假核算列表
     */
    @SaCheckPermission("attendance:annualLeave:export")
    @Log(title = "年假核算", businessType = BusinessType.EXPORT, linkType = "annualLeave")
    @PostMapping("/export")
    public void export(AnnualLeaveBo bo, HttpServletResponse response) {
        List<AnnualLeaveVo> list = annualLeaveService.queryList(bo);
        ExcelUtil.exportExcel(list, "年假核算", AnnualLeaveVo.class, response);
    }

    /**
     * 获取年假核算详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("attendance:annualLeave:query")
    @GetMapping("/{id}")
    public R<AnnualLeaveVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(annualLeaveService.queryById(id));
    }

    /**
     * 新增年假核算
     */
    @SaCheckPermission("attendance:annualLeave:add")
    @Log(title = "年假核算", businessType = BusinessType.INSERT, linkType = "annualLeave")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AnnualLeaveBo bo) {
        return toAjax(annualLeaveService.insertByBo(bo));
    }

    /**
     * 修改年假核算
     */
    @SaCheckPermission("attendance:annualLeave:edit")
    @Log(title = "年假核算", businessType = BusinessType.UPDATE, linkType = "annualLeave")
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AnnualLeaveBo bo) {
        return toAjax(annualLeaveService.updateByBo(bo));
    }

    /**
     * 删除年假核算
     *
     * @param ids 主键串
     */
    @SaCheckPermission("attendance:annualLeave:remove")
    @Log(title = "年假核算", businessType = BusinessType.DELETE, linkType = "annualLeave")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(annualLeaveService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 年度核算 - 为所有在职员工核算年假
     *
     * @param year 年份
     */
    @SaCheckPermission("attendance:annualLeave:calculate")
    @Log(title = "年度年假核算", businessType = BusinessType.OTHER, linkType = "annualLeave")
    @PostMapping("/calculateYear")
    public R<Void> calculateYear(@NotNull(message = "年份不能为空") @RequestParam Integer year) {
        annualLeaveService.calculateAnnualLeaveForYear(year);
        return R.ok();
    }

    /**
     * 为单个员工核算年假
     *
     * @param employeeNo 工号
     * @param year       年份
     */
    @SaCheckPermission("attendance:annualLeave:calculate")
    @Log(title = "员工年假核算", businessType = BusinessType.OTHER, linkType = "annualLeave")
    @PostMapping("/calculateEmployee")
    public R<Void> calculateEmployee(
            @NotNull(message = "工号不能为空") @RequestParam String employeeNo,
            @NotNull(message = "年份不能为空") @RequestParam Integer year) {
        annualLeaveService.calculateAnnualLeaveForEmployee(employeeNo, year);
        return R.ok();
    }

    /**
     * 手动调整年假天数
     */
    @SaCheckPermission("attendance:annualLeave:adjust")
    @Log(title = "年假调整", businessType = BusinessType.UPDATE, linkType = "annualLeave")
    @PostMapping("/adjust")
    public R<Void> adjust(@RequestBody AnnualLeaveAdjustBo adjustBo) {
        return toAjax(annualLeaveService.adjustAnnualLeave(adjustBo));
    }

    /**
     * 查询年假调整日志
     *
     * @param annualLeaveId 年假记录ID
     */
    @SaCheckPermission("attendance:annualLeave:query")
    @GetMapping("/adjustLogs/{annualLeaveId}")
    public R<List<AnnualLeaveAdjustLogVo>> getAdjustLogs(@PathVariable Long annualLeaveId) {
        return R.ok(annualLeaveService.queryAdjustLogs(annualLeaveId));
    }
}
